﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Solidinfo.Web.Helpers
{
    /// <summary>
    /// 指定某Action需求权限
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class RequiredAccessAttribute : ActionFilterAttribute
    {
        private string[] _accessTypeArray;
        /// <summary>
        /// 构建函数
        /// </summary>
        /// <param name="accessNames">需求的权限</param>
        public RequiredAccessAttribute( params string[] accessNames)
        {
            _accessTypeArray = accessNames;
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var user = filterContext.Controller.GetCurrentUser();
            if (user == null)
            {
                filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { Controller = "Home", action = "Login" }));
            }
            else
            {
                //此处验证该用户的权限
                if (user.Permissions != null)
                {

                    var accessNameList = user.Permissions;
                    if (accessNameList.Count > 0)
                    {
                        bool check = true;
                        foreach (var item in _accessTypeArray)
                        {
                            check = check & accessNameList.Contains(item);
                        }
                        if (check)
                        {
                            base.OnActionExecuting(filterContext);
                            return;
                        }
                    }

                }
                //此处为无权限，可将其重定向到无权限页面
                var contentResult = new ContentResult()
                {
                    Content = "您没有足够权限访问此页面",
                };
                filterContext.Result = contentResult;// new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { Controller = "Admin", action = "NoAccess" }));
            }

        }
    }
    /// <summary>
    /// 指定某Action需求用户登录
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class RequiredLoginAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var user = filterContext.Controller.GetCurrentUser();
            if (user == null)
            {
                filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { Controller = "Home", action = "Login" }));
            }
            base.OnActionExecuting(filterContext);
        }
    }

    //public static class QcAccessHtmlHelper
    //{
    //    /// <summary>
    //    /// 判断是否拥有权限
    //    /// </summary>
    //    /// <param name="htmlHelper">htmlHelper</param>
    //    /// <param name="accessId">模块ID</param>
    //    /// <param name="accessType">权限</param>
    //    /// <returns>是否拥有该权限</returns>
    //    public static bool QcIsExistAccess(this HtmlHelper htmlHelper, AccessType accessType, int accessId)
    //    {
    //        var employee = htmlHelper.ViewContext.HttpContext.Session.GetCurrrentEmployee();
    //        if (employee != null)
    //        {
    //            if (employee.AccessItems != null)
    //            {
    //                var item = employee.AccessItems.Where(e => e.Modal.AccessId == accessId).FirstOrDefault();
    //                if (item != null)
    //                {
    //                    if ((item.Access & accessType) == accessType)
    //                    {
    //                        return true;
    //                    }
    //                }
    //            }
    //        }
    //        return false;
    //    }
    //    /// <summary>
    //    /// 判断是否拥有任意一个AccessId的某个权限
    //    /// </summary>
    //    /// <param name="htmlHelper">htmlHelper</param>
    //    /// <param name="accessType">权限</param>
    //    /// <param name="accessIds">模块ID列表</param>
    //    /// <returns>是否拥有该权限</returns>
    //    public static bool QcIsExistAccess(this HtmlHelper htmlHelper, AccessType accessType, params int[] accessIds)
    //    {
    //        foreach (var accessId in accessIds)
    //        {
    //            var exist = htmlHelper.QcIsExistAccess(accessType, accessId);
    //            if (exist)
    //            {
    //                return true;
    //            }
    //        }
    //        return false;
    //    }

    //    /// <summary>
    //    /// 如果拥有该权限则输出内容，否则输出空文本
    //    /// </summary>
    //    /// <param name="htmlHelper">htmlHelper</param>
    //    /// <param name="modalId">模块ID</param>
    //    /// <param name="accessType">权限</param>
    //    /// <param name="mvcHtmlString">待输出的内容</param>
    //    /// <returns>输出内容</returns>
    //    public static MvcHtmlString QcAccessFor(this HtmlHelper htmlHelper, AccessType accessType, int modalId, MvcHtmlString mvcHtmlString)
    //    {
    //        if (htmlHelper.QcIsExistAccess(accessType, modalId))
    //        {
    //            return mvcHtmlString;
    //        }
    //        else
    //        {
    //            return MvcHtmlString.Empty;
    //        }
    //    }

    //    /// <summary>
    //    /// 如果拥有该权限则输出内容，否则输出空文本
    //    /// </summary>
    //    /// <param name="htmlHelper">htmlHelper</param>
    //    /// <param name="modalId">模块ID</param>
    //    /// <param name="accessType">权限</param>
    //    /// <param name="contentString">待输出的内容</param>
    //    /// <returns>输出内容</returns>
    //    public static MvcHtmlString QcAccessFor(this HtmlHelper htmlHelper, AccessType accessType, int modalId, string contentString)
    //    {
    //        if (htmlHelper.QcIsExistAccess(accessType, modalId))
    //        {
    //            return new MvcHtmlString(contentString);
    //        }
    //        else
    //        {
    //            return MvcHtmlString.Empty;
    //        }
    //    }
    //    /// <summary>
    //    /// 如果拥有该组下的任何List权限，返回True，否则返回False
    //    /// </summary>
    //    /// <param name="htmlHelper">htmlHelper</param>
    //    /// <param name="groupId">模块群组</param>
    //    /// <returns>是否拥有该权限</returns>
    //    public static bool QcIsExistGroupListAccess(this HtmlHelper htmlHelper, int groupId)
    //    {
    //        return QcIsExistGroupAccess(htmlHelper, AccessType.List, groupId);
    //    }

    //    /// <summary>
    //    /// 如果拥有该组下的任何指定权限，返回True，否则返回False
    //    /// </summary>
    //    /// <param name="htmlHelper">htmlHelper</param>
    //    /// <param name="groupId">模块群组</param>
    //    /// <returns>是否拥有该权限</returns>
    //    public static bool QcIsExistGroupAccess(this HtmlHelper htmlHelper, AccessType accessType, int groupId)
    //    {
    //        var employee = htmlHelper.ViewContext.HttpContext.Session.GetCurrrentEmployee();
    //        if (employee != null)
    //        {
    //            if (employee.AccessItems != null)
    //            {
    //                var list = employee.AccessItems.Where(e => e.Modal.GroupdId == groupId).ToList();
    //                foreach (var item in list)
    //                {
    //                    if ((item.Access & accessType) == accessType)
    //                    {
    //                        return true;
    //                    }
    //                }
    //            }
    //        }
    //        return false;
    //    }
    //    /// <summary>
    //    /// 如果拥有该组下的任何指定权限，返回True，否则返回False
    //    /// </summary>
    //    /// <param name="htmlHelper">htmlHelper</param>
    //    /// <param name="groupIds">模块群组列表</param>
    //    /// <returns>是否拥有该权限</returns>
    //    public static bool QcIsExistGroupAccess(this HtmlHelper htmlHelper, AccessType accessType, params int[] groupIds)
    //    {
    //        foreach (var id in groupIds)
    //        {
    //            var exist = htmlHelper.QcIsExistGroupAccess(accessType, id);
    //            if (exist)
    //            {
    //                return true;
    //            }
    //        }
    //        return false;
    //    }
    //    /// <summary>
    //    /// 如果拥有该组下的任何List权限，返回True，否则返回False
    //    /// </summary>
    //    /// <param name="htmlHelper">htmlHelper</param>
    //    /// <param name="modalGroupId">模块群组列表</param>
    //    /// <returns>是否拥有该权限</returns>
    //    public static bool QcIsExistGroupListAccess(this HtmlHelper htmlHelper, params int[] groupIds)
    //    {
    //        foreach (var id in groupIds)
    //        {
    //            var exist = htmlHelper.QcIsExistGroupAccess(AccessType.List, id);
    //            if (exist)
    //            {
    //                return true;
    //            }
    //        }
    //        return false;
    //    }
    //}
}